Orphan Instances
孤立した「型クラスのinstance宣言」のこと
classを定義したModuleと、data型を定義したModuleの両方の外部で定義されたinstanceのこと
「Orphan」の直訳は「孤児」
pursならerror
hsではwarning
例
以下のように定義した場合、showHogeはOrphan Instancesとなる
↓はpursのコードなので、↓はerrorになるmrsekut.icon
code:Class.purs(hs)
-- classの定義
module Class where
class MyShow a where
myShow :: a -> String
code:data.purs(hs)
-- data型の定義
module Data where
data Hoge = Hoge
code:orphan.purs(hs)
-- instanceの定義
module Orphan where
instance showHoge :: MyShow Hoge where
myShow _ = "hoge"
instance showHoge ..の記述を、ClassかDataのいずれかに移せばOrphan Instanceではなくなり、errorは解消される
参考
documentation/Type-Classes.md at master · purescript/documentation
pursのdocs
https://mizunashi-mana.github.io/blog/posts/2020/03/coherent-typeclass-and-orphan-instance/#orphan
https://wiki.haskell.org/Orphan_instance
http://maoe.hatenadiary.jp/entry/20100902/1283358286